package com.itheima.reggie.service.impl;

import cn.hutool.core.util.IdUtil;
import com.itheima.reggie.common.ResultInfo;
import com.itheima.reggie.domain.Category;
import com.itheima.reggie.exception.CustomException;
import com.itheima.reggie.mapper.CategoryMapper;
import com.itheima.reggie.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
@Transactional
public class CategoryServiceImpl implements CategoryService {

    @Autowired
    private CategoryMapper categoryMapper;

    @Override
    public List<Category> findAll() {
        return categoryMapper.findAll();
    }

    @Override
    public void add(Category category) {
        /*category.setId(IdUtil.getSnowflake(1, 1).nextId());
        category.setCreateTime(new Date());
        category.setCreateUser(1L);
        category.setUpdateTime(new Date());
        category.setUpdateUser(1L);*/
        categoryMapper.insert(category);
    }

    @Override
    public void update(Category category) {
//        //todo  临时写死
//        category.setUpdateTime(new Date());
//        category.setUpdateUser(1L);
        categoryMapper.updateById(category);
    }

    @Override
    public void delete(Long id) {

        //统计菜品分类下的菜品数量
        Integer num1 = categoryMapper.countDishByCategoryId(id);
        if (num1 > 0) {
            throw new CustomException("此分类下还有菜品，不允许删除");
        }
        //统计套餐分类下的套餐数量
        Integer num2 = categoryMapper.countSetMealByCategoryId(id);

        if (num2 > 0) {
            throw new CustomException("此分类下存在套餐，不允许删除");
        }
        categoryMapper.delete(id);
    }

    @Override
    public List<Category> findListByType(Integer type) {
        List<Category> list = categoryMapper.selectByType(type);
        return list;
    }

    /*@Override
    public ResultInfo delete(Long id) {

        //统计菜品分类下的菜品数量
        Integer num1 = categoryMapper.countDishByCategoryId(id);
        if (num1 > 0) {
            return ResultInfo.error("此分类下还有菜品，不允许删除");
        }
        //统计套餐分类下的套餐数量
        Integer num2 = categoryMapper.countSetMealByCategoryId(id);

        if(num2 > 0){
            return ResultInfo.error("此分类下存在套餐，不允许删除");
        }

        categoryMapper.delete(id);
        return ResultInfo.success(null);
    }*/
}
